using Abp.Authorization;
using System.Linq;
using System.Reflection;

namespace AbpProjectTemplate.Components.Authorization
{
    public class AppAuthorizationProvider : AuthorizationProvider
    {
        public override void SetPermissions(IPermissionDefinitionContext context)
        {
            //自动注册当前程序集中所有定义了AppPermissionAttribute的枚举
            var permissionTypes = this.GetType()
                .Assembly
                .GetTypes()
                .Where(t => t.IsEnum && t.IsDefined(typeof(PermissionDefineAttribute)));
            foreach (var type in permissionTypes)
            {
                var define = PermissionHelper.GetPermissionDefine(type);
                var parentPermission = context.CreatePermission(define.Name);
                parentPermission.Fill(define);
                //处理枚举值
                foreach (var field in type.GetFields().Where(t => t.Name != "value__"))
                {
                    var cDefine = PermissionHelper.GetPermissionDefine(field);
                    var childPermission = parentPermission.CreateChildPermission(cDefine.Name);
                    childPermission.Fill(cDefine);
                }
            }
        }
    }
}


